package cn.csust.net2.manager.server.service.impl;


import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.csust.net2.manager.server.dao.VoteAnswerDAO;
import cn.csust.net2.manager.server.dao.VoteDAO;
import cn.csust.net2.manager.server.dao.VoteResultDAO;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Vote;
import cn.csust.net2.manager.shared.po.VoteAnswer;
import cn.csust.net2.manager.shared.po.VoteResult;
import cn.csust.net2.manager.shared.service.VoteCommonService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;


/*
 * 此类为vote 和 votemanager 的公共服务类
 */
@Service(value = ServiceConstant.SERVICE_NAME_VOTECOMMON)

public class VoteCommonServiceImpl extends TreeServiceImpl implements VoteCommonService{
@Resource(name=Constant.DAO_NAME_VOTEANSWER)
	private VoteAnswerDAO voteAnswerDAO;
	//	@Resource(name = DAOConstant.SESSION_FACTORY)
//	private SessionFactory sessionFactory;
	@Resource(name=Constant.DAO_NAME_VOTE)
	private VoteDAO voteDAO;
	@Resource(name=Constant.DAO_NAME_VOTERESULT)
	private VoteResultDAO voteResultDAO;

	@Transactional
	@Override
	public PagingLoadResult<Long> findStudent_Vote(PagingLoadConfig config)throws BeyondAuthorityException {
		Long voteID = config.get(VOTEID);
		String query = "select studentNumber from student_vote where voteID = "+voteID;
		return 	voteAnswerDAO.findAllByPagingBySQL(config,query, Long.class);
	}
	@Transactional
	@Override
	public PagingLoadResult<VoteAnswer> findVoteAnswersByPaging(
			PagingLoadConfig config) throws BeyondAuthorityException{
		Long voteID = config.get(VOTEID);
		if(voteID !=null&&!voteID.equals("")){
			
			return 	voteAnswerDAO.findAllByPaging(config, " from VoteAnswer c where c.vote.voteId = "+voteID);
		
		}else{
			
			return 	this.findAll(VoteAnswer.class.getName(), config);
		}
	}
	@Transactional
	@Override
	public int findVoteResultsByAnswerID(Long l) throws BeyondAuthorityException{
		// TODO Auto-generated method stub
		if(l>0)
			return (int) voteAnswerDAO.countsByHQL(" from VoteResult v where v.answer.answerId = "+l);
		else
			return 0;
	}
	@Transactional
	@Override
	public PagingLoadResult<VoteResult> findVoteResultsByPaging(
			PagingLoadConfig config) throws BeyondAuthorityException{
		Long voteID = config.get(VOTEID);
		if(voteID !=null&&!voteID.equals("")){
			
			return 	voteResultDAO.findAllByPaging(config, " from VoteResult c where c.vote.voteId = "+voteID);
		
		}else{
			
			return 	this.findAll(VoteResult.class.getName(), config);
		}
	}

	@Transactional
	@Override
	public PagingLoadResult<Vote> findVotesByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		Long studentNumber = config.get(STUDENT_NUMBER);
		Timestamp startTime = config.get(START_TIME);
		Timestamp endTime = config.get(END_TIME);
		StringBuffer sql = new StringBuffer("select * from vote v where 1 = 1");
		if(studentNumber!=null&&!studentNumber.equals(""))
			sql.append(" and v.voteId in ( select voteId from Student_Vote s where s.StudentNumber = " +studentNumber+ " ) ");
		if(startTime!=null&&!startTime.equals(""))
			sql.append(" and v.startTime = "+startTime);
		if(endTime!=null&&!endTime.equals(""))
			sql.append(" and v.endTime = "+endTime);
		
		 return voteDAO.findAllByPagingBySQL(config, sql.toString(), Vote.class);
	}

	public VoteAnswerDAO getVoteAnswerDAO() {
		return voteAnswerDAO;
	}

	public VoteDAO getVoteDAO() {
		return voteDAO;
	}
	public VoteResultDAO getVoteResultDAO() {
		return voteResultDAO;
	}
	
	@Transactional
	@Override
	public boolean hasVoteResultsByStudnetNumber(Long l)throws BeyondAuthorityException {
		// TODO Auto-generated method stub
		long count = voteAnswerDAO.countsBySQL(" select counts(*) from student_vote where studentNumber = "+l);
		if(count>0)
			return true;
		else
			return false;
	}
	@Transactional
	@Override
	public <T extends PO> BasePagingLoadResult<T> findAll(String cla,PagingLoadConfig config) throws BeyondAuthorityException {
		//获得VOTE创建者的ID
		Long userId  = config.get(Constant.PUBUSERID);
		Integer banjiID = config.get(Constant.BANJI_ID);
		StringBuffer sb = new StringBuffer(" select *  from Vote c where 1=1 ");
		if(banjiID!=null){
			Timestamp d = new Timestamp(System.currentTimeMillis()); 
			sb.append(" and  c.startTime <= '"+d+"' and c.endTime >= '"+d+"' and c.voteID in ( select voteID  from banji_vote where banjiID = "+banjiID+" ) ");
			return 	getVoteResultDAO().findAllByPagingBySQL(config, sb.toString(), Vote.class);
		}
		if(userId!=null){
			
			return getVoteResultDAO().findAllByPaging(config, " from Vote c where c.pubUserId = "+userId);
			
		}else{
			
			return super.findAll(cla, config);
		}
	}

//	@Transactional
//	@Override
//	public void saveStudent_VoteByPaging(PagingLoadConfig config) {
//		Long voteID = config.get(VOTEID);
//		List<Long>	numberList = config.get(STUDENT_NUMBERL_LIST);
//		if(voteID !=null&&!voteID.equals("")&&numberList!=null){
//			for(Long numberID:numberList){
//				String sql = "INSERT INTO `manager`.`student_vote`(`studentNumber`,`voteID`) VALUES ( "+numberID+","+voteID+");";
//				voteAnswerDAO.getCurrentSession().createSQLQuery(sql);
//			}
//		
//		}
//	}
	public void setVoteAnswerDAO(VoteAnswerDAO voteAnswerDAO) {
		this.voteAnswerDAO = voteAnswerDAO;
	}
	public void setVoteDAO(VoteDAO voteDAO) {
		this.voteDAO = voteDAO;
	}
	public void setVoteResultDAO(VoteResultDAO voteResultDAO) {
		this.voteResultDAO = voteResultDAO;
	}

}
